﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Red.CommUtils.Application
{
    /// <summary>
    /// remenber to sync it with the same enum on the c++ engine side
    /// </summary>
    enum EServiceCommand
    {
        None,
        Success,
        Failed
    }

    /// <summary>
    /// Handle to the application. Use wisely. When out of scope, will release remote application handle (sends message to the application),
    /// so application is closed when there are no more references (handles).
    /// </summary>
    public interface IAppHandle
    {
        string Name { get; }
        string DisplayName { get; }
        string CrashFilePath { get; }
        IPAddress Address { get; }
        AppChannels Channels { get; }

        /// <summary>
        /// Process is killed.
        /// </summary>
        void ForceClose();

        /// <summary>
        /// Restore application in case of some failure.
        /// </summary>
        /// <returns>True if successfully restored.</returns>
        Task<bool> Restore();

        void RequestToolkitService();
        void RequestClose();
    }

    /// <summary>
    /// All possible channels for the app handle.
    /// </summary>
    public class AppChannels
    {
        private IChannel _main;
        private IChannel _debug;
        private IChannel _editor;

        public IChannel Main
        {
            get { return _main; }
        }

        public IChannel Debug
        {
            get { return _debug; }
        }

        public IChannel Editor
        {
            get { return _editor; }
        }

        public AppChannels( IChannel editor )
        {
            _main = null;
            _debug = null;
            _editor = editor;
        }

        internal void InitMainChannel( IChannel main )
        {
            _main = main;
        }

        internal void InitDebugChannel( IChannel debug )
        {
            _debug = debug;
        }
    }

    /// <summary>
    /// Launches applications on platform (i.e. implementation for XBox One would launch application on XBox One with specified IP address)
    /// </summary>
    public interface IAppLauncher
    {
        string CrashFilePath { get; }
        Task<IAppHandle> Launch( string[] arguments );
    }

    /// <summary>
    /// Application launcher exception wrapper.
    /// </summary>
    public class AppLauncherException : Exception
    {
        public AppLauncherException( string message )
            : base( message )
        {

        }
    }
}
